package com.mfl.controller.admin;

import com.mfl.common.PageResult;
import com.mfl.common.Result;
import com.mfl.pojo.dto.PostDTO;
import com.mfl.pojo.dto.PostPageQueryDTO;
import com.mfl.pojo.vo.PostVO;
import com.mfl.service.PostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminPostController")
@RequestMapping("/cky/admin/post")
@Slf4j
public class PostController {

    @Autowired
    private PostService postService;

    /**
     * 分页查询帖子
     * @param pageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(PostPageQueryDTO pageQueryDTO){
        log.info("分页查询帖子：{}", pageQueryDTO);
        PageResult pageResult = postService.pageQuery(pageQueryDTO);
        return Result.success("", pageResult);
    }

    /**
     * 新增帖子
     * @param postDTO
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody PostDTO postDTO){
        log.info("新增帖子：{}", postDTO);
        postService.insert(postDTO);
        return Result.success("新增成功");
    }

    /**
     * 批量删除帖子
     * @param ids
     * @return
     */
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestParam List<Integer> ids){
        log.info("批量删除帖子：{}", ids);
        postService.batchDelete(ids);
        return Result.success("删除成功");
    }

    /**
     * 根据id查询帖子
     * @param id
     * @return
     */
    @GetMapping("/getPostById")
    public Result<PostVO> getPostById(int id){
        log.info("查询帖子：{}", id);
        PostVO postVO = postService.getPostById(id);
        return Result.success("", postVO);
    }

    /**
     * 修改帖子
     * @param postDTO
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody PostDTO postDTO){
        log.info("修改帖子：{}", postDTO);
        postService.update(postDTO);
        return Result.success("修改成功");
    }
}
